package com.tongsys.server.config.security.exception;

import com.alibaba.fastjson.JSON;
import com.tongsys.server.utils.RtnUtils;
import com.tongsys.server.utils.WebUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
@ApiOperation("自定义 用户认证 的异常类")
public class MyAuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {

        RtnUtils rtn = RtnUtils.err().code(HttpStatus.FORBIDDEN.value()).msg("认证失败");
        String jsonString = JSON.toJSONString(rtn);
        WebUtils.renderString(response,jsonString);
    }
}
